var mySwiper2 = new Swiper('.swiper2', {
    direction: 'horizontal',
    autoplay: false,
    // 如果需要分页器
    pagination: {
        el: '.swiper-pagination',
        clickable: true
    }
});


$(function () {
    setTimeout(function () {
        /**初始看板数据**/
        initBoard();
    }, 200);

    if (!isChart)
        loadReport();

    /**客户资料**/
    $(".client-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**原材管理**/
    $(".materials-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });
    /**车辆排队**/
    $(".vehicle-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**原材抽检**/
    $(".check-btn").on("click", function () {
        //wapAlert("功能暂未开放")
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**手机订单**/
    $(".phone-order-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**外部订单**/
    $(".out-order-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**方量查询**/
    $(".volume-search-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**材料库存**/
    $(".stock-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**监控配比**/
    $(".ratio-btn").on("click", function () {
        //wapAlert("功能暂未开放")
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放")
        else
            window.location.href = url;
    });

    /**销售管理**/
    $(".contract-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放");
        else
            window.location.href = url;
    });
});

/**加载报表**/
function loadReport() {
    // wapLoading.show();
    $.ajax({
        type: "POST",
        url: "/volume/query/today/chart",
        async: true,
        dataType: 'json',
        timeout: 15000,
        success: function(res) {
            // wapLoading.hide();
            console.log(res)
            if(res.code == 0) {
                myChart = echarts.init(document.getElementById('main'));
                chartOption = res.data;
                myChart.setOption(chartOption);
            } else {
                defaultOption();
            }

        },
        error: function(res) {
            // wapAlert(res.msg);
            // wapLoading.hide();
            return;
        },
        complete: function(XMLHttpRequest, status) {
            if(status == 'timeout') {
                wapAlert('网络链接超时,请稍后再试');
                // wapLoading.hide();
                return;
            }
        }
    });
}

function defaultOption() {
    myChart = echarts.init(document.getElementById('main'));
    var option = {
        title: {
            text: '数据加载错误',
            subtext: '2021-01-01(系统异常)',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: ['计划', '生产', '发货', '签收', '退回', '转出', '报废'],
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '单位:方',
                type: 'bar',
                barWidth: '60%',
                data: [{
                    value: 0,
                    itemStyle: {
                        color: '#546FC6'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#FBB141'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#86B96F'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#67B47F'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#FDC937'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#FD7E56'
                    }
                }, {
                    value: 0,
                    itemStyle: {
                        color: '#ABBF56'
                    }
                }]
            }
        ]
    };

    option && myChart.setOption(option);
}

/**初始看板数据**/
function initBoard() {
    $.ajax({
        type: "post",
        url: '/volume/query/board',
        async: true,
        dataType: 'json',
        timeout: 15000,
        success: function(res) {
            console.log(res)
            if(res.code == 0) {
                $('.today-deliver-volume').html(res.data.today.deli);
                $('.today-plan-volume').html(res.data.today.plans);
                $('.today-task-num').html(res.data.today.taskNum);
                $('.today-train-num').html(res.data.today.trainNum);

                $('.yesterday-deliver-volume').html(res.data.yesterday.deli);
                $('.yesterday-plan-volume').html(res.data.yesterday.plans);
                $('.yesterday-task-num').html(res.data.yesterday.taskNum);
                $('.yesterday-train-num').html(res.data.yesterday.trainNum);
            }
        },
        error: function(res) {
            // wapAlert(res.msg);
            return;
        },
        complete: function(XMLHttpRequest, status) {
            if(status == 'timeout') {
                wapAlert('网络链接超时,请稍后再试');
                return;
            }
        }
    });
}